home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / mint / mint104s.zoo / mint.src / signal.c < prev    next >
C/C++ Source or Header  |  1993-03-08  |  17KB  |  614 lines

  1. /*
  2. Copyright 1990,1991,1992 Eric R. Smith.
  3. Copyright 1992 Atari Corporation.
  4. All rights reserved.
  5. */
  6.  
  7. /* signal.c:: signal handling routines */
  8.  
  9. #include "mint.h"
  10.  
  11. void (*sig_routine)();    /* used in intr.s */
  12. short sig_exc;        /* used in intr.s */
  13.  
  14. /*
  15.  * killgroup(pgrp, sig): send a signal to all members of a process group
  16.  * returns 0 on success, or an error code on failure
  17.  */
  18.  
  19. long
  20. killgroup(pgrp, sig)
  21.     int pgrp, sig;
  22. {
  23.     PROC *p;
  24.     int found = 0;
  25.  
  26.     TRACE(("killgroup %d %d", pgrp, sig));
  27.  
  28.     if (pgrp < 0)
  29.         return EINTRN;
  30.  
  31.     for (p = proclist; p; p = p->gl_next) {
  32.         if (p->pgrp == pgrp) {
  33.             post_sig(p, sig);
  34.             found++;
  35.         }
  36.     }
  37.     if (found) {
  38.         check_sigs();    /* see if the current process is affected */
  39.         return 0;
  40.     }
  41.     else {
  42.         DEBUG(("killgroup: no processes found"));
  43.         return EFILNF;
  44.     }
  45. }
  46.  
  47. /* post_sig: post a signal as being pending. It is assumed that the
  48.    caller has already verified that "sig" is a valid signal, and
  49.    moreover it is the caller's responsibility to call check_sigs()
  50.    if it's possible that p == curproc
  51.  */
  52.  
  53. void
  54. post_sig(p, sig)
  55.     PROC *p;
  56.     int sig;
  57. {
  58.     ulong sigm;
  59.  
  60. /* if process is ignoring this signal, do nothing
  61.  * also: signal 0 is SIGNULL, and should never be delivered through
  62.  * the normal channels (indeed, it's filtered out in dossig.c,
  63.  * but the extra sanity check here is harmless). The kernel uses
  64.  * signal 0 internally for some purposes, but it is handled
  65.  * specially (see supexec() in xbios.c, for example).
  66.  */
  67.     if (p->sighandle[sig] == SIG_IGN || sig == 0)
  68.         return;
  69.  
  70. /* if the process is already dead, do nothing */
  71.     if (p->wait_q == ZOMBIE_Q || p->wait_q == TSR_Q)
  72.         return;
  73.  
  74. /* mark the signal as pending */
  75.     sigm = (1L << (unsigned long)sig);
  76.     p->sigpending |= sigm;
  77.  
  78. /* if the signal is masked, do nothing further */
  79.     if ( (p->sigmask & sigm) != 0 )
  80.         return;
  81.  
  82. /* otherwise, make sure the process is awake */
  83.     if (p->wait_q && p->wait_q != READY_Q) {
  84.         short sr = spl7();
  85.         rm_q(p->wait_q, p);
  86.         add_q(READY_Q, p);
  87.         spl(sr);
  88.     }
  89. }
  90.  
  91. /*
  92.  * check_sigs: see if we have any signals pending. if so,
  93.  * handle them.
  94.  */
  95.  
  96. void
  97. check_sigs()
  98. {
  99.     ulong sigs, sigm;
  100.     int i;
  101.     short deliversig;
  102.  
  103.     if (curproc->pid == 0) return;
  104. top:
  105.     sigs = curproc->sigpending & ~(curproc->sigmask);
  106.     if (sigs) {
  107.         sigm = 2;
  108. /* with tracing we need a mechanism to allow a signal to be delivered
  109.  * to the child (curproc); Fcntl(...TRACEGO...) passes a SIGNULL to indicate that we
  110.  * should really deliver the signal, hence its always safe to remove it
  111.  * from pending.
  112.  */
  113.         deliversig = (curproc->sigpending & 1L);
  114.         curproc->sigpending &= ~1L;
  115.  
  116.         for (i = 1; i < NSIG; i++) {
  117.             if (sigs & sigm) {
  118.                 curproc->sigpending &= ~sigm;
  119.                 if (curproc->ptracer && !deliversig) {
  120.                     TRACE(("tracer being notified of signal %d", i));
  121.                     stop(i);
  122.         /* the parent may reset our pending signals, so check again */
  123.                     goto top;
  124.                 } else {
  125.                     ulong omask;
  126.  
  127.                     curproc->sigpending &= ~sigm;
  128.                     omask = curproc->sigmask;
  129.  
  130.         /* sigextra gives which extra signals should also be masked */
  131.                     curproc->sigmask |= curproc->sigextra[i] | sigm;
  132.                     handle_sig(i);
  133.  
  134.  
  135. /*
  136.  * POSIX.1-3.3.4.2(723) "If and when the user's signal handler returns
  137.  * normally, the original signal mask is restored."
  138.  *
  139.  * BUG?: This unmasking could unmask a pending signal which we will not
  140.  * see this time around (if the signal number is less than i) and which
  141.  * was not pending when we started; should we detect this condition and
  142.  * loop around for a second try? POSIX only guarantees delivery of
  143.  * one signal per kernel entry, so this shouldn't really be a problem.
  144.  */
  145.                     curproc->sigmask = omask;    /* unmask signals */
  146.                 }
  147.             }
  148.             sigm = sigm << 1;
  149.         }
  150.     }
  151. }
  152.  
  153. /*
  154.  * raise: cause a signal to be raised in the current process
  155.  */
  156.  
  157. void
  158. raise(sig)
  159.     int sig;
  160. {
  161.     post_sig(curproc, sig);
  162.     check_sigs();
  163. }
  164.  
  165. #ifdef EXCEPTION_SIGS
  166. /* exception numbers corresponding to signals */
  167. char excep_num[NSIG] =
  168. { 0, 0, 0, 0,
  169.   4,            /* SIGILL == illegal instruction */
  170.   9,            /* SIGTRAP == trace trap    */
  171.   4,            /* pretend SIGABRT is also illegal instruction */
  172.   8,            /* SIGPRIV == privileged instruction exception */
  173.   5,            /* SIGFPE == divide by zero */
  174.   0, 2,            /* SIGBUS == bus error */
  175.   3            /* SIGSEGV == address error */
  176. /* everything else gets zeros */
  177. };
  178.  
  179. /* a "0" means we don't print a message when it happens -- typically the
  180.    user is expecting a synchronous signal, so we don't need to report it
  181. */
  182.  
  183. const char *signames[NSIG] = { 0,
  184. 0, 0, 0, "ILLEGAL INSTRUCTION", "TRACE TRAP",
  185. 0, "PRIVILEGE VIOLATION", "DIVISION BY ZERO", 0, "BUS ERROR",
  186. "ADDRESS ERROR", "BAD SYSTEM CALL", 0, 0, 0,
  187. 0, 0, 0, 0, 0,
  188. 0, 0, 0, "CPU TIME EXHAUSTED", "FILE TOO BIG",
  189. 0, 0, 0, 0, 0
  190. };
  191.  
  192. /*
  193.  * replaces the TOS "show bombs" routine: for now, print the name of the
  194.  * interrupt on the console, and save info on the crash in the appropriate
  195.  * system area
  196.  */
  197.  
  198. void
  199. bombs(sig)
  200.     int sig;
  201. {
  202.     long *procinfo = (long *)0x380L;
  203.     int i;
  204.     CONTEXT *crash;
  205.     extern int no_mem_prot;
  206.  
  207.     if (sig < 0 || sig > 31) {
  208.         ALERT("bombs(%d): sig out of range", sig);
  209.     }
  210.     else if (signames[sig]) {
  211.         if (!no_mem_prot && sig == SIGBUS) {
  212.             /* already reported by report_buserr */
  213.         } else {
  214.             ALERT("%s: User PC=%lx (basepage=%lx)",
  215.                 signames[sig],
  216.                 curproc->exception_pc, curproc->base);
  217.         }
  218. /* save the processor state at crash time */
  219. /* assumes that "crash time" is the context curproc->ctxt[SYSCALL] */
  220. /* BUG: this is not true if the crash happened in the kernel; in the
  221.  * latter case, the crash context wasn't saved anywhere.
  222.  */
  223.         crash = &curproc->ctxt[SYSCALL];
  224.         *procinfo++ = 0x12345678L;    /* magic flag for valid info */
  225.         for (i = 0; i < 15; i++)
  226.             *procinfo++ = crash->regs[i];
  227.         *procinfo++ = curproc->exception_ssp;
  228.         *procinfo++ = ((long)excep_num[sig]) << 24L;
  229.         *procinfo = crash->usp;
  230.  
  231. /* we're also supposed to save some info from the supervisor stack. it's not
  232.  * clear what we should do for MiNT, since most of the stuff that used to be
  233.  * on the stack has been put in the CONTXT struct. Moreover, we don't want
  234.  * to crash because of an attempt to access illegal memory. Hence, we do
  235.  * nothing here...
  236.  */
  237.     } else {
  238.         TRACE(("bombs(%d)", sig));
  239.     }
  240. }
  241. #endif
  242.  
  243. /*
  244.  * handle_sig: do whatever is appropriate to handle a signal
  245.  */
  246.  
  247. static long unwound_stack = 0;
  248.  
  249. void
  250. handle_sig(sig)
  251.     int sig;
  252. {
  253.     long oldstack, newstack;
  254.     long *stack;
  255.     CONTEXT *call, oldsysctxt, newcurrent;
  256.     extern void sig_return();
  257.  
  258.     if (curproc->sighandle[sig] == SIG_IGN)
  259.         return;
  260.     else if (curproc->sighandle[sig] == SIG_DFL) {
  261. _default:
  262.         switch(sig) {
  263. #if 0
  264. /* Note: SIGNULL is filtered out in dossig.c and is never actually
  265.  * delivered (its only purpose for the user is to test for the existence of
  266.  * a process, it isn't a real signal). The kernel uses SIGNULL
  267.  * internally, but all such code does the signal handling "by hand"
  268.  * and so no default handling is necessary.
  269.  */
  270.         case SIGNULL:
  271. #endif
  272.         case SIGWINCH:
  273.         case SIGCHLD:
  274. /* SIGFPE is divide by 0; TOS ignores this, so we will too */
  275.         case SIGFPE:
  276.             return;        /* do nothing */
  277.         case SIGSTOP:
  278.         case SIGTSTP:
  279.         case SIGTTIN:
  280.         case SIGTTOU:
  281.             stop(sig);
  282.             return;
  283.         case SIGCONT:
  284.             curproc->sigpending &= ~STOPSIGS;
  285.             return;
  286.  
  287. /* here are the fatal signals. for SIGINT, we use p_term(-32) so that
  288.  * TOS programs that catch ^C via the vector at 0x400 and which expect
  289.  * TOS's error code (-32) to be sent will work. For most other signals,
  290.  * we p_term with an error code; for SIGKILL, we don't want to allow
  291.  * the program any chance to recover, so we call terminate() directly
  292.  * to avoid calling through to the user's terminate vector.
  293.  */
  294.         case SIGINT:        /* ^C */
  295.             if (curproc->domain == DOM_TOS) {
  296.                 p_term(-32);
  297.                 return;
  298.             }
  299.             /* otherwise, fall through */
  300.         default:
  301. #ifdef EXCEPTION_SIGS
  302.             bombs(sig); /* tell the user what happened */
  303. #endif
  304.     /* the "sigmask" check is in case a bus error happens in the user's
  305.      * term_vec